<?php //pack11class

class DekaroObject {
    
    private $properties=array();
    private $photos=array();
    public $traceEnabled=true;
    //public $special=false;
    public $id;
    public $usedCurrency=1;
    public $label="";
    public $labelSet=false;
    public $active=false;
    public $mapdata=false;
    public $description=array();

    public function __set($name,$value) {
        if(@$property=clone PropertiesManager::getProperty($name)) {
            $property->value=$value;
            $this->properties[$name]=$property;
        }
    }

    public function __get($name) {
        if(!array_key_exists($name, $this->properties)) {
            //trace("Wrong property name {$name} in DekaroObject");
            return false;
        }
        return $this->properties[$name];
    }

    public function __toString() {
        $printed="Object {$this->id}\n";
        foreach($this->properties as $name=>$property) {
            $printed.=$name."=".$property->value."\n";
        }
        return get_class($this);
    }

    public function finalizeProperties(){
        $strValue="Error";
        $units="Error";
        $this->photos=array();
        foreach($this->properties as $property) {
            if($property->isValue) {
                if($property->group=="price") {
                    $usedCurrency=CurrencyManager::getCurrency($this->properties["object_currency"]->value);
                    if(get_class($usedCurrency)=="Currency") {
                        $this->usedCurrency=$usedCurrency;
                        $strValue=CurrencyManager::getValue($property->value,$usedCurrency->id);
                        $units=CurrencyManager::currentCurrency()->label;
                    }
                    if($property->system=="price") $units.="/".LabelManager::getLabel("price_sqm");
                    $property->string=$strValue." ".$units;
                } else if($property->group=="media") {
                    if($property->value) array_push($this->photos,$property);
                    $property->string=$property->value;
                } else {
                    $property->string=$property->value;
                }
            } else if ($property->hasValue){
                $objectRow=MySQLGet::process("Select `prop".$property->id."` as `value` from ".MySQL::TABLE_OBJECTS." where `id`='{$this->id}' limit 1;");
                $strValue=$property->value;
                //$sql=MySQLGet::process("Select ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `id`='".$objectRow[0]['value']."' limit 1;");
                //$units=$sql[0]["value"];
                $units=ValuesManager::getValueByID($objectRow[0]['value']);
                $strValue.=" ".$units;
                $property->string=$strValue;
            } else {
                /*$sql=MySQLGet::process("Select ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `id`='{$property->value}' limit 1;");
                $property->string=$sql[0]["value"];*/
                if($property->system=="special") {
                    $spectmp=ValuesManager::getValueByID($property->value);
                    $cid=MySQLGet::process("Select ".LanguagesManager::sql()." from `d12_specials` where LCASE(".LanguagesManager::ssql().")='".strtolower($spectmp)."' OR `id`='".$property->value."' limit 1;");
                    if(MySQLGet::$cc==0) {
                        trace("NULL");
                    } else {
                        //trace($property->value." = ".$cid[0]["id"]);
                        $property->string=$cid[0]["value"];
                    }
                } else {
                    $property->string=ValuesManager::getValueByID($property->value);
                }
                $property->city24=ValuesManager::getValueByID($property->value,ValuesManager::TYPE_CITY24);
            }

            if($property->combined) {
                $comby=explode(",",$property->combined);
                if(array_key_exists($comby[0], $this->properties)) {
                    $prop=$this->properties[$comby[0]];
                    $prop->string.=$comby[1].$property->string;
                }
            }
        }
    }

    public function usedProperties() {
        return $this->properties;
    }

    public function getAnotherPrice(Property $property) {
        $group=PropertiesManager::getGroup($property->groupNum);
        $value=0;
        foreach($group as $current) {
            if($current->system!=$property->system) {
                $objectRow=MySQLGet::process("Select `prop".$current->id."` as `value` from ".MySQL::TABLE_OBJECTS." where `id`='{$this->id}' limit 1;");
                $value=$objectRow[0]['value'];
            }
            //trace($current->system." ".$property->system." ".$value);
        }
        return $value;
    }

    public function getPhotos() {
        return $this->photos;
    }

    public function getMoney($price=11) {
        $properties=$this->properties;
        if(!is_bool($price)) {
            $showPrice=false;
            $sp=SettingsManager::getSetting("showPrice");
            if($sp) $showPrice=($sp->value==0)? false:true;
        } else {
            $showPrice=$price;
        }
        if($showPrice) {
            if(array_key_exists("price", $properties)) {
                if($properties["price"]->value==-1) {
                    if(array_key_exists("cost", $properties)) {
                        //trace("--------------->>>>>>>>>>>>");
                        return $properties["cost"]->string.ObjectsListComments::setComment(LabelManager::getLabel("comment_cost"));
                    } else {
                        return false;
                    }
                } else {
                    return $properties["price"]->string;
                }
            } else if(array_key_exists("cost", $properties)) {
                if(array_key_exists("square", $properties)) {
                    $units=CurrencyManager::currentCurrency()->label;
                    $priceValue=CurrencyManager::getValue(($properties["cost"]->value/$properties["square"]->value),$this->usedCurrency->id,true);
                    return $priceValue." ".$units."/".LabelManager::getLabel("price_sqm");
                } else {
                    if(is_bool($price)) {
                        return false;
                    } else {
                        return $properties["cost"]->string.ObjectsListComments::setComment(LabelManager::getLabel("comment_cost"));
                    }
                }
            } else {
                return false;
            }
        } else {
            if(array_key_exists("cost", $properties)) {
                return $properties["cost"]->string;
            } else {
                return false;
            }
        }

        return false;
    }
    
    public function getLabel($setRoot=false) {

        if($this->labelSet) return $this->label;

        $properties=$this->usedProperties();
        $propertiesArray=array();
        foreach($properties as $property) {
            if($setRoot) {
                $rootProperty=PropertiesManager::getProperty($property->id);
                foreach($rootProperty as $prop=>$is) $rootProperty->$prop=$property->$prop;
            }
            if(!$property->combined && $property->group=="address") {
                array_push($propertiesArray,$property->string);
            }
        }

        $this->labelSet=true;
        $userin=SettingsManager::getSetting("userin");
        $idText="";
        if($userin->value && !array_key_exists("preview",$_GET)) $idText="[".$this->id."] ";
        $this->label=$idText.implode(", ",$propertiesArray);
        return $this->label;

    }

}
?>
